package com.zggk.lzz.roadz;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.zggk.lzz.R;
import com.zggk.lzz.adapter.LGRecycleViewAdapter;
import com.zggk.lzz.adapter.LGViewHolder;
import com.zggk.lzz.api.MyApplication;
import com.zggk.lzz.bridget.BridgeTActivity;
import com.zggk.lzz.dtfd.DtfdActivity;
import com.zggk.lzz.land.LandActivity;
import com.zggk.lzz.lzml.LzmlActivity;
import com.zggk.lzz.mvp.MVPBaseActivity;
import com.zggk.lzz.roadt.RoadTActivity;
import com.zggk.lzz.sjcx.SjcxActivity;
import com.zggk.lzz.sjxq.SjxqActivity;
import com.zggk.lzz.tunnelt.TunnelTActivity;
import com.zggk.lzz.updata.UpdateManager;
import com.zggk.lzz.xcjl.XcjlActivity;
import com.zggk.lzz.xxyd.XxydActivity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class RoadZActivity extends MVPBaseActivity<RoadZContract.View, RoadZPresenter> implements RoadZContract.View {

    @Bind(R.id.home_title)
    TextView homeTitle;
    @Bind(R.id.cancle_button)
    TextView cancleButton;
    @Bind(R.id.header_lay)
    RelativeLayout headerLay;
    @Bind(R.id.name_img)
    ImageView nameImg;
    @Bind(R.id.name_te)
    TextView nameTe;
    @Bind(R.id.lz_img)
    ImageView lzImg;
    @Bind(R.id.name_lay)
    LinearLayout nameLay;
    @Bind(R.id.xj_te)
    TextView xjTe;
    @Bind(R.id.lxlc_te)
    TextView lxlcTe;
    @Bind(R.id.lxlcs_te)
    TextView lxlcsTe;
    @Bind(R.id.lxlc_re)
    RelativeLayout lxlcRe;
    @Bind(R.id.qlsl_te)
    TextView qlslTe;
    @Bind(R.id.qlsls_te)
    TextView qlslsTe;
    @Bind(R.id.qlsl_re)
    RelativeLayout qlslRe;
    @Bind(R.id.sdsl_te)
    TextView sdslTe;
    @Bind(R.id.sdsls_te)
    TextView sdslsTe;
    @Bind(R.id.sdsl_re)
    RelativeLayout sdslRe;
    @Bind(R.id.two_zxzrs)
    TextView twoZxzrs;
    @Bind(R.id.two_dqzx)
    TextView twoDqzx;
    @Bind(R.id.two_jrzx)
    TextView twoJrzx;
    @Bind(R.id.two_lx)
    TextView twoLx;
    @Bind(R.id.two_cql)
    TextView twoCql;
    @Bind(R.id.cklb_te)
    TextView cklbTe;
    @Bind(R.id.sb_re)
    RecyclerView sbRe;
    @Bind(R.id.xcjl_im)
    ImageView xcjlIm;
    @Bind(R.id.dtfb_im)
    ImageView dtfbIm;
    @Bind(R.id.lzml_img)
    ImageView lzmlImg;
    @Bind(R.id.flfg_img)
    ImageView flfgImg;
    @Bind(R.id.activity_disease_new_scrollview)
    ScrollView activityDiseaseNewScrollview;
    @Bind(R.id.disease_new_parent_layout)
    RelativeLayout diseaseNewParentLayout;
    @Bind(R.id.ckwzmd_te)
    TextView ckwzmdTe;
    @Bind(R.id.wzxx_te)
    TextView wzxxTe;
    private LGRecycleViewAdapter<RoadZthreeInfo.DataBean> adapter;
    private List<RoadZthreeInfo.DataBean> ListDatas = new ArrayList<>();
    private Date sss;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initView();
        initData();
        new UpdateManager(this).checkUpdate();   //检查更新
    }

    @Override
    protected int getLayout() {
        return R.layout.act_road_z;
    }

    private void initView() {
        nameTe.setText(MyApplication.spUtils.getString("UserName"));
    }

    private void initData() {
        mPresenter.getOne(MyApplication.spUtils.getString("OrgId"), RoadZActivity.this);

    }

    @Override
    public void onRequestError(String msg) {

    }

    @Override
    public void getOnes(RoadZoneInfo.DataBean dataBean) {
        lxlcsTe.setText(dataBean.getRecords().getBuildLength());
        qlslsTe.setText(dataBean.getRecords().getBridgeCount());
        sdslsTe.setText(dataBean.getRecords().getTunnelCount());
        mPresenter.getTwo();
    }

    @Override
    public void getTwos(RoadZtwoInfo.DataBean dataBean) {
        xjTe.setText(MyApplication.spUtils.getString("OrgName"));
        wzxxTe.setText(MyApplication.spUtils.getString("DemonstrationUnit"));
        homeTitle.setText(MyApplication.spUtils.getString("OrgName"));
        twoZxzrs.setText("在线总人数：" + dataBean.getRecords().getLznum() + "人");
        twoDqzx.setText(dataBean.getRecords().getNowline());
        twoJrzx.setText(dataBean.getRecords().getDayline());
        twoLx.setText("离线：" + (Integer.valueOf(dataBean.getRecords().getLznum()) - Integer.valueOf(dataBean.getRecords().getNowline())));
        twoCql.setText(dataBean.getRecords().getFindpro() + "%");
        mPresenter.getThree();
    }

    @Override
    public void getThrees(RoadZthreeInfo dataBean) {
        ListDatas.addAll(dataBean.getData());
        sbRe.setHasFixedSize(true);
        sbRe.setNestedScrollingEnabled(false);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        sbRe.setLayoutManager(manager);
        adapter = new LGRecycleViewAdapter<RoadZthreeInfo.DataBean>(ListDatas) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_three;
            }

            @Override
            public void convert(LGViewHolder holder, RoadZthreeInfo.DataBean zCDBean, int position) {
                holder.setText(R.id.time_te, getDateTimeFromMillisecond(zCDBean.getCreateTime()+""));
                holder.setText(R.id.dz_te, zCDBean.getRoadName() + zCDBean.getRoadCode());
                holder.setText(R.id.zt_te, zCDBean.getEtype() + " " + zCDBean.getHandle());
            }
        };
        sbRe.setAdapter(adapter);
        adapter.setOnItemClickListener(R.id.item_layout, new LGRecycleViewAdapter.ItemClickListener() {
            @Override
            public void onItemClicked(View view, int position) {
                Intent intent = new Intent(RoadZActivity.this, SjxqActivity.class);
                intent.putExtra("id", ListDatas.get(position).getId());
                startActivity(intent);
            }
        });
    }

    public static String getDateTimeFromMillisecond(String millisecond) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZ");
        Date date = new Date();
        try {
            date = sdf.parse(millisecond);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sDate=sdf2.format(date);
        return sDate;
    }

    @OnClick({R.id.cklb_te, R.id.cancle_button, R.id.lxlc_re, R.id.qlsl_re, R.id.sdsl_re,
            R.id.xcjl_im, R.id.dtfb_im, R.id.lzml_img, R.id.flfg_img, R.id.ckwzmd_te})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cklb_te:
                Intent intent2 = new Intent(RoadZActivity.this, SjcxActivity.class);
                startActivity(intent2);
                break;
            case R.id.cancle_button:
                MyApplication.spUtils.clear();
                Intent intent = new Intent(RoadZActivity.this, LandActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.lxlc_re:
                startActivity(new Intent(RoadZActivity.this, RoadTActivity.class));
                break;
            case R.id.qlsl_re:
                startActivity(new Intent(RoadZActivity.this, BridgeTActivity.class));
                break;
            case R.id.sdsl_re:
                startActivity(new Intent(RoadZActivity.this, TunnelTActivity.class));
                break;
            case R.id.xcjl_im:
                startActivity(new Intent(RoadZActivity.this, XcjlActivity.class));
                break;
            case R.id.dtfb_im:
                startActivity(new Intent(RoadZActivity.this, DtfdActivity.class));
                break;
            case R.id.lzml_img:
                startActivity(new Intent(RoadZActivity.this, LzmlActivity.class));
                break;
            case R.id.ckwzmd_te:
                startActivity(new Intent(RoadZActivity.this, LzmlActivity.class));
                break;
            case R.id.flfg_img:
                startActivity(new Intent(RoadZActivity.this, XxydActivity.class));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
